package web;


import com.smart.domain.Teacher;
import com.smart.service.TeacherServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
public class TeacherController {



   private TeacherServiceImp teacherServiceImp;



    @RequestMapping(value = "/index.html")
    public String loginPage(){
        return "login";
    }

    @RequestMapping(value = "/loginCheck.html")
    public ModelAndView loginCheck(HttpServletRequest request,Logincommand logincommand) {
        Teacher teacher1=new Teacher();
        teacher1.setUserName(logincommand.getTeacherusername());
       // System.out.println(loginCommand.getUserNamecom());
        teacher1.setPassword(logincommand.getTeacherpassword());
       // System.out.println(loginCommand.getPasswordcom());
        boolean isValidteacher = teacherServiceImp.hasMatchTeacher(teacher1);
        System.out.println(isValidteacher);
        if (!isValidteacher) {
            return new ModelAndView("login", "error", "用户名或密码错误。");

        } else {
            teacher1.setUserName(logincommand.getTeacherusername());
             Teacher teacher =teacherServiceImp.queryteacher(teacher1);
            System.out.println(teacher);
            request.getSession().setAttribute("teacher", teacher);
            return new ModelAndView("home");
        }


    }


    @RequestMapping(value= "index1.html")
    public String Add(){
        return "AddTeacher";
    }
//增加老师信息返回信息页面
    @RequestMapping(value = "/AddTeacher.html")
    public  ModelAndView  AddTeacher( HttpServletRequest request, HttpServletResponse response,Logincommand loginCommand ){
      Teacher teacher =new Teacher();
      teacher.setName(loginCommand.getTeachername());
      teacher.setDepartment(loginCommand.getTeacherdepartment());
      teacher.setUserName(loginCommand.getTeacherusername());
      teacher.setPassword(loginCommand.getTeacherpassword());
      teacher.setEmail(loginCommand.getTeacheremail());
      teacher.setPhoneNumber(loginCommand.getTeacherphonenumber());


        teacherServiceImp.addteacher(teacher);
        Teacher teacher1 =teacherServiceImp.queryteacher(teacher);
        request.getSession().setAttribute("teacher", teacher1);
        return new ModelAndView("home");
    }

    //删除老师信息



    @Autowired
    public void setTeacherServiceImp(TeacherServiceImp teacherServiceImp) {
        this.teacherServiceImp = teacherServiceImp;
    }


}

